﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using Midapex.SmartOrm.Tools.Wizard.Properties;

namespace Midapex.SmartOrm.Tools.Wizard
{
    public partial class MySqlConnForm : Form
    {
        public MySqlConnForm()
        {
            InitializeComponent();
        }

        internal string GetConnString()
        {
            string connString = string.Format("Data Source={0};User Id={1};Password={2};",
                    textBoxServer.Text, userTextBox.Text, pwdTextBox.Text);

            if(dbComboBox.Text != null && dbComboBox.Text != string.Empty)
            {
                connString = string.Format("{0}Database={1};", connString, dbComboBox.Text);
            }

            if(textBoxPort.Text !="3306")
            {
                connString = string.Format("{0}Port={1};", connString, textBoxPort.Text);
            }

            return connString;
        }

        private void ConnDb()
        {
                if(string.IsNullOrEmpty(textBoxServer.Text))
                    throw new Exception(Resources.EmptyServerName);

                OrmSessionsFactory.SetConfig(DataSourceType.MySql, GetConnString());

                OrmSession session = OrmSessionsFactory.CreateSession();

                using (DbConnection conn = session.CreateDbConnection())
                {
                    conn.Open();
                    dbComboBox.Items.Clear();

                    string sqlCmd = "SHOW DATABASES";
                    DbCommand cmd = session.CreateDbCommand(sqlCmd);
                    cmd.Connection = conn;

                    IDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        if ("information_schema" == reader.GetString(0))
                            continue;
                        dbComboBox.Items.Add(reader.GetString(0));
                    }

                    if (dbComboBox.Items.Count != 0)
                    {
                        dbComboBox.SelectedIndex = 0;
                    }
                }
        }

        private void btnCreateNewDB_Click(object sender, EventArgs e)
        {
            try
            {
                OrmSession session = OrmSessionsFactory.CreateSession();
                string newDbName = dbComboBox.Text;
                string sqlCmd = "create database " + newDbName;
                session.ExecuteNonQuery(sqlCmd);
                dbComboBox.ResetText();

                ConnDb();
                dbComboBox.Text = newDbName;

                MessageBox.Show(this, Resources.ConnDataSourceSuccess,Resources.Information,
                    MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Resources.Warning,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void testConnButton_Click_1(object sender, EventArgs e)
        {
            try
            {
                ConnDb();
                
                MessageBox.Show(this, Resources.ConnDataSourceSuccess,
                        Resources.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Resources.Warning,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MySqlConnForm_Load(object sender, EventArgs e)
        {
            this.textBoxServer.Focus();
        }
    }
}